import requests
import json

DEEPSEEK_API_KEY = "sk-vhqnvfrzrfqrichlptnuyzrbasnqowkfalkpmtmildygqmwe"
API_URL = "https://api.siliconflow.cn/v1/chat/completions"

def load_data():
    """Cong wenjian duqu chuanganqi shuju """
    try:
        with open("env_data.json") as f:
            return json.load(f)
    except FileNotFoundError:
        print("Cuowu: Wei zhaodao shuju wenjian ")
        exit(1)

def get_ai_advice(data):
    """Huode AI zhongzhi jianyi """
    headers = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}"}
    
    
    prompt = f"""DANGQIAN SHUJU :
    - wendu :{data['temp']}C 
    - shidu :{data['humidity']}% 
    - qiya :{data['pressure']}hPa
    
    Qing yong zhongwen gei chucao mei zhongzhi jianyi :
    1. Fenxi huanjing youdian quedian (<50 zi)
    2. Tuijian caozuo (tongfeng/ guangai/ buguang)
    Lizi :Fenxi... | Jianyi:..."""
    
    try:
        response = requests.post(
            API_URL,
            headers=headers,
            json={
                "model": "deepseek-ai/DeepSeek-R1",
                "messages": [{
                    "role": "user",
                    "content": prompt + "\n\n"  
                }],
                "temperature": 0.7
            },
            timeout=10
        )
        return response.json()['choices'][0]['message']['content']
    except Exception as e:
        return f"API qingqiu shibai :{str(e)}"

if __name__ == "__main__":
    env_data = load_data()
    advice = get_ai_advice(env_data)
    
    print("\n=== Chuanganqi Shuju ===")
    print(f"Wendu : {env_data['temp']}C")
    print(f"Shidu : {env_data['humidity']}%")
    print(f"Qiya : {env_data['pressure']}hPa")
    
    print("\n=== AI Jianyi ===")
    print(advice.replace("|", "\n"))